package com.roy.wakeupes.service.impl;




import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.roy.wakeupes.mapper.HotelMapper;
import com.roy.wakeupes.pojo.Hotel;

import com.roy.wakeupes.pojo.HotelDoc;
import com.roy.wakeupes.service.IHotelService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


/**
 * @description: ES 服务实现
 * @author: AmazeCode
 * @date: 2023/11/18 21:34
 */
@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {

    @Autowired
    private RestHighLevelClient client;

    @Override
    public void insertByIdBatch() {

        try {
            BulkRequest bulkRequest = new BulkRequest();
            List<Hotel> list = this.list();
            for (Hotel hotel : list) {
                HotelDoc hotelDoc = new HotelDoc(hotel);
                IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
                request.source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON);
                bulkRequest.add(request);
            }
            BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println(bulk.status());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
